package com.youlu.campus.service.activity;


import com.youlu.campus.common.utils.DateTimeUtils;
import com.youlu.campus.entity.ActivityUserRecord;
import com.youlu.campus.entity.ActivityUserRecordNew;
import com.youlu.campus.entity.VO.req.SignUpDataMigrationReq;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ActivityUserRecordService {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MapperFacade mapperFacade;

    public void signUpDataMigration(SignUpDataMigrationReq signUpDataMigrationReq) {
        String activityId = signUpDataMigrationReq.getActivityId();
        String startTime = signUpDataMigrationReq.getStartTime();
        String endTime = signUpDataMigrationReq.getEndTime();
        log.info("【数据迁移】start activityId ->{} | startTime ->{} | endTime ->{}", activityId, startTime, endTime);
        Date startDate = null;
        Date endDate = null;
        if (StringUtils.isAllBlank(activityId, startTime, endTime)) {
            log.error("【数据迁移】 activityId ->{} | startTime ->{} | endTime ->{} is empty", activityId, startTime,
                    endTime);
            return;
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            startDate = DateTimeUtils.StringToDate(startTime);
            endDate = DateTimeUtils.StringToDate(endTime);
        }
        Integer pageNo = 1;
        Integer pageSize = 10000;
        while (true) {
            log.info("【数据迁移】running page：{} activityId:{} | startTime:{} | endTime:{}", pageNo, activityId,
                    startTime,
                    endTime);
            Query query = new Query();
            if (StringUtils.isNotBlank(activityId)) {
                query.addCriteria(Criteria.where("activityId").is(activityId));
            }
            if (Objects.nonNull(startDate) && Objects.nonNull(endDate)) {
                query.addCriteria(Criteria.where("createdTime").gte(startDate).lte(endDate));
            }
            Pageable pageRequest = PageRequest.of(pageNo - 1, pageSize);
            List<ActivityUserRecord> activityUserList = this.mongoTemplate.find(query.with(pageRequest),
                    ActivityUserRecord.class);
            if (CollectionUtils.isEmpty(activityUserList)) {
                log.info("【数据迁移】running end page：{} activityId:{} | startTime:{} | endTime:{}", pageNo, activityId,
                        startTime,
                        endTime);
                break;
            }
            List<ActivityUserRecordNew> userRecordNewList = mapperFacade.mapAsList(activityUserList,
                    ActivityUserRecordNew.class);
            List<ActivityUserRecordNew> list =
                    userRecordNewList.stream().peek(activityUserRecordNew -> activityUserRecordNew.setId(null)).collect(Collectors.toList());
            this.mongoTemplate.insertAll(list);
            userRecordNewList.clear();
            list.clear();
            pageNo++;
        }
        log.info("【数据迁移】end activityId ->{} | startTime ->{} | endTime ->{}", activityId, startTime, endTime);
    }


}
